package com.junsi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 审批角色控制器
 * @author lenovo
 *
 */
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.ApproveRole;
import com.junsi.bean.ApproveTail;
import com.junsi.bean.Staff;
import com.junsi.service.ApproveRoleService;
import com.junsi.service.ApproveTailService;
import com.junsi.service.StaffSerivce;

import net.sf.json.JSONArray;
@Controller
@RequestMapping("/approveRole")
public class ApproveRoleController {
	@Autowired
	private ApproveRoleService approveRoleService;
	@Autowired
	private ApproveTailService approveTailService;
	@Autowired
	private StaffSerivce staffSerivce;
	Logger logger = Logger.getRootLogger();
	/**
	 * 刪除
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(Integer id, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		approveRoleService.delete(id);
		modelAndView.setViewName("redirect:/approveRole/select.action");
		return modelAndView;
		
	}
	
	/**
	 * 新增
	 */
	@RequestMapping("/insert")
	public ModelAndView insert(ApproveRole approveRole, HttpServletResponse response) throws Exception {
		String approver=approveRole.getApprover();
		 if(approver!=null){
				approveRoleService.insert(approveRole);
		 }			 
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/approveRole/select.action");
		return modelAndView;

	}
	/**
	 * 更新
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updata")
	@ResponseBody
	public ModelAndView updata(ApproveRole approveRole, HttpServletResponse response) throws Exception {	
		ModelAndView modelAndView = new ModelAndView();
		if (approveRole.getApprover() != null && approveRole.getApprover()!=null) {
			approveRoleService.update(approveRole);
			ApproveTail approveTail = new ApproveTail();
			approveTail.setApproverID(approveRole.getApprover());
			approveTail.setApproveType(approveRole.getTypeObject());
			approveTail.setApproveRoleId(approveRole.getApproveRoleId().toString());
			approveTailService.update(approveTail);
		}

		modelAndView.setViewName("redirect:/approveRole/select.action");
		return modelAndView;
	}
	/**
	 * 查询
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/select")
	@ResponseBody
	public ModelAndView select( HttpServletResponse response){
		ModelAndView modelAndView = new ModelAndView();
		List<ApproveRole> approveRoles = approveRoleService.listAll();
		List<Staff> staffs = staffSerivce.findByAll();
		ApproveRoleController.arrayapprover(approveRoles, staffs);
		modelAndView.addObject("approveRoles", approveRoles);
		modelAndView.setViewName("forward:/page/liuchengguanli.jsp");
		return modelAndView;
	}
	/**
	 * 查询流程名称
	 */
	@RequestMapping("/findflowNme")
	@ResponseBody
	public void findflowNme( HttpServletResponse response){
		  List<ApproveRole> approveRoles=approveRoleService.listAll();
		  List<Map<String, String>> array = new ArrayList<Map<String, String>>();
			for (ApproveRole arrayList : approveRoles) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("approveRoleId", arrayList.getApproveRoleId().toString());
				map.put("flowNme", arrayList.getFlowNme());
				array.add(map);
			}
			JSONArray jsonArray = JSONArray.fromObject(array);
			response.setContentType("text/html; charset=UTF-8");
			try {
				response.getWriter().write(jsonArray.toString());
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	/**
	 * 员工查询
	 * @param response
	 */
	@RequestMapping("/findStaff")
	@ResponseBody
	public void findStaff( HttpServletResponse response){
		List<Staff> staffs=staffSerivce.findByAll();
		List<Map<String, String>> array = new ArrayList<Map<String, String>>();
		for (Staff arrayList : staffs) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("permissionID", arrayList.getStaff_id().toString());
			map.put("permissionName", arrayList.getStaff_name());
			array.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(array);
		response.setContentType("text/html; charset=UTF-8");
		try {
			response.getWriter().write(jsonArray.toString());
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 查询
	 */
	@RequestMapping("/findId")
	public void findId(String id, HttpServletResponse response) {
		ApproveRole approveRole = approveRoleService.findById(Integer.valueOf(id));
		List<Staff> staffs = staffSerivce.findByAll();
		String[] arrayApprover = approveRole.getApprover().split(",");
		String approverName = null;
			for (int i = 0; i < arrayApprover.length; i++) {
				for (Staff arrayList : staffs) {
					String name=arrayList.getStaff_id().toString();
				if (name.equals(arrayApprover[i])) {// 审批人
					if (i == 0) {
						approverName = arrayList.getStaff_name();
					} else {
						approverName += "," + arrayList.getStaff_name();
					}
				}
			}
		}
		approveRole.setApprover(approverName);
		JSONArray jsonArray = JSONArray.fromObject(approveRole);
		response.setContentType("text/html; charset=UTF-8");
		try {
			response.getWriter().write(jsonArray.toString());
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	 public static void  arrayapprover(List<ApproveRole> approveRoles,List<Staff> staffs){
		for (ApproveRole approveRole : approveRoles) {
			String approverName = null;
			if (approveRole.getApprover() != null || !"".equals(approveRole.getApprover())) {
				String[] arrayApprover = approveRole.getApprover().split(",");
				for (int i = 0; i < arrayApprover.length; i++) {
					for (Staff arrayList : staffs) {
						String name = arrayList.getStaff_id().toString();
						if (name.equals(arrayApprover[i])) {// 审批人
							if (i == 0) {
								approverName = arrayList.getStaff_name();
							} else {
								approverName += " , " + arrayList.getStaff_name();
							}
						}
					}
				}

			}
			approveRole.setApprover(approverName);
		}
	 }
	}

